#include "head.h"


int en_task_queue(task_queue_t* ptask_queue, client_info_t* pclient_info){
    //创建并初始化新节点
    task_t* newNode = (task_t*)calloc(1,sizeof(task_t));
    newNode->client_info.net_fd = pclient_info->net_fd;
    strcpy(newNode->client_info.name,pclient_info->name);
    strcpy(newNode->client_info.command,pclient_info->command);
    strcpy(newNode->client_info.argument,pclient_info->argument);
    newNode->next = NULL;
    
    //把新节点加入到队列
    if(ptask_queue->size == 0){
        ptask_queue->head = newNode;
        ptask_queue->end = newNode;
        ptask_queue->size++;
    }
    else{
        ptask_queue->end->next = newNode;
        ptask_queue->end = newNode;
        ptask_queue->size++;
    }

    return 0;
}
int de_task_queue(task_queue_t* ptask_queue, client_info_t* pclient_info){
    if(ptask_queue->size == 0){
        return -1;
    }
    task_t* cur = ptask_queue->head;
    //头指针向后移
    ptask_queue->head = ptask_queue->head->next;
    if(ptask_queue->size == 1){
        //如果队列只有一个元素，尾指针也向后移（即尾指针指向NULL）
        ptask_queue->end = ptask_queue->end->next;
    }
    ptask_queue->size--;
    //把队头元素的内容复制，返回出去
    pclient_info->net_fd = cur->client_info.net_fd;
    strcpy(pclient_info->name,cur->client_info.name);
    strcpy(pclient_info->command,cur->client_info.command);
    strcpy(pclient_info->argument,cur->client_info.argument);
    free(cur);

    return 0;
}
